package com.example.myapplication.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.MyApplication;
import com.example.myapplication.R;
import com.example.myapplication.adapter.MyFragmentPagerAdapter;
import com.example.myapplication.bean.LotDevice;
import com.example.myapplication.listener.MyPageChangeListner;
import com.example.myapplication.util.OrderedProperties;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vpager;
    private RadioGroup tabRg;
    private RadioButton tabRbYangan;
    private RadioButton tabRbLudeng;
    private RadioButton tabRbJingai;
    private RadioButton tabRbRenti;
    private RadioButton tabRbNongye;
    private ListView listView;
    private FragmentManager fm;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private Dialog loadingDialog;

    //设备列表
    private List<LotDevice> list = new ArrayList<>();


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    private void initData() {
        fm = getSupportFragmentManager();
        OrderedProperties props = ((MyApplication) getApplication()).getProps();
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(fm,
                MyFragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, props);
        vpager.setAdapter(myFragmentPagerAdapter);
        vpager.addOnPageChangeListener(new TabChangeListener());
        tabRg.setOnCheckedChangeListener(new TabCheckChangelistener());
    }

    private void initView() {
        vpager = (ViewPager) findViewById(R.id.vpager);
        tabRg = (RadioGroup) findViewById(R.id.tab_rg);
        tabRbYangan = (RadioButton) findViewById(R.id.tab_rb_yangan);
        tabRbLudeng = (RadioButton) findViewById(R.id.tab_rb_ludeng);
        tabRbJingai = (RadioButton) findViewById(R.id.tab_rb_jingai);
        tabRbRenti = (RadioButton) findViewById(R.id.tab_rb_renti);
        tabRbNongye = (RadioButton) findViewById(R.id.tab_rb_nongye);
        listView = (ListView) findViewById(R.id.list_view);
    }

    /**
     * tab联动fragment
     */
    class TabCheckChangelistener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.tab_rb_yangan:
                    vpager.setCurrentItem(0);
                    break;
                case R.id.tab_rb_ludeng:
                    vpager.setCurrentItem(1);
                    break;
                case R.id.tab_rb_jingai:
                    vpager.setCurrentItem(2);
                    break;
                case R.id.tab_rb_renti:
                    vpager.setCurrentItem(3);
                    break;
                case R.id.tab_rb_nongye:
                    vpager.setCurrentItem(4);
                    break;
            }
        }
    }

    /**
     * fragment联动tab
     */
    class TabChangeListener extends MyPageChangeListner {

        @Override
        public void setTab(int postion) {
            switch (postion) {
                case 0:
                    tabRbYangan.setChecked(true);
                    break;
                case 1:
                    tabRbLudeng.setChecked(true);
                    break;
                case 2:
                    tabRbJingai.setChecked(true);
                    break;
                case 3:
                    tabRbRenti.setChecked(true);
                    break;
                case 4:
                    tabRbNongye.setChecked(true);
                    break;
            }
        }
    }
}